package com.supermarket.order.controller;

import com.supermarket.common.core.domain.R;
import com.supermarket.common.core.web.controller.BaseController;
import com.supermarket.common.core.web.page.TableDataInfo;
import com.supermarket.common.log.annotation.Log;
import com.supermarket.common.log.enums.BusinessType;
import com.supermarket.common.security.annotation.RequiresPermissions;
import com.supermarket.order.dto.OrderDeliveryDTO;
import com.supermarket.order.dto.OrderQueryDTO;
import com.supermarket.order.dto.UpdateRemarkDTO;
import com.supermarket.order.service.OrderService;
import com.supermarket.order.vo.OrderDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @RequiresPermissions("order:list")
    @Operation(summary = "订单分页多条件查询")
    @GetMapping("/list")
    public TableDataInfo list(OrderQueryDTO queryDTO) {
        startPage();
        return getDataTable(orderService.getOrderList(queryDTO));
    }

    @RequiresPermissions("order:query")
    @Operation(summary = "订单详情")
    @GetMapping("/{id}")
    public R<OrderDetailVO> detail(@PathVariable Long id) {
        return R.ok(orderService.getOrderDetail(id));
    }

    @RequiresPermissions("order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "关闭订单")
    @PostMapping("/close")
    public R<?> close(@RequestParam Long orderId, @RequestParam String note) {
        orderService.closeOrder(orderId, note);
        return R.ok();
    }
    
    @RequiresPermissions("order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "订单发货")
    @PostMapping("/deliver")
    public R<?> deliver(@RequestBody OrderDeliveryDTO deliveryDTO) {
        orderService.deliverOrder(deliveryDTO);
        return R.ok();
    }
    
    @RequiresPermissions("order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改备注")
    @PostMapping("/remark")
    public R<?> updateRemark(@RequestBody UpdateRemarkDTO remarkDTO) {
        orderService.updateRemark(remarkDTO);
        return R.ok();
    }
} 